package com.registration.controller;

import com.registration.Annotation.OperateLog;
import com.registration.bean.PatientBean;
import com.registration.service.PatientService;
import com.registration.util.JsonResponse;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin
public class PatientController {

    @Autowired
    private PatientService patientService;



    /**
     * 获取就诊人列表
     * 对应文档：GET /api/v1/patient
     */
    @GetMapping("/patient")
    @OperateLog("获取就诊人列表")
    public JsonResponse getPatientList(HttpServletRequest request) {
        // 实际场景：从Token解析userId，此处简化为请求参数
        Integer userId = Integer.parseInt(request.getParameter("user_id"));
        List<PatientBean> patients = patientService.getPatientsByUserId(userId);
        return JsonResponse.success(patients);
    }

    /**
     * 获取单个就诊人详情
     * 对应文档：GET /api/v1/patient/{id}
     */
    @GetMapping("/patient/{patientId}")
    @OperateLog("获取就诊人详情")
    public JsonResponse getPatientDetail(@PathVariable Integer patientId) {
        // 实际需补充根据patientId查询详情的Service和Mapper方法
        return JsonResponse.success(null); // 简化实现，真实场景需完善
    }

    @PostMapping("/patient")
    public JsonResponse addPatient(@RequestBody PatientBean patientBean) {
        // 添加就诊人信息
        int result = patientService.insertPatient(patientBean);
        if (result > 0) {
            return JsonResponse.success(result, "添加成功");
        } else {
            return JsonResponse.fail("添加失败");
        }
    }


    @DeleteMapping("/patient/{patientId}")
    public JsonResponse deletePatient(@PathVariable Integer patientId) {
        // 删除就诊人信息
        int result = patientService.deletePatient(patientId);
        if (result > 0) {
            return JsonResponse.success(result, "删除成功");
        } else {
            return JsonResponse.fail("删除失败");
        }
    }


    @PutMapping("/patient")
    public JsonResponse updatePatient(@RequestBody PatientBean patientBean) {
        // 更新就诊人信息
        int result = patientService.updatePatient(patientBean);
        if (result > 0) {
            return JsonResponse.success(result, "更新成功");
        } else {
            return JsonResponse.fail("更新失败");

        }
    }


    @PutMapping("/patient/default")
    public JsonResponse updateDefaultPatient(@RequestBody PatientBean patientBean) {
        int result = patientService.updateDefaultPatient(patientBean.getUser_id(), patientBean.getPatient_id());
        if (result > 0) {
            return JsonResponse.success(result, "更新成功");
        } else {
            return JsonResponse.fail("更新失败");
        }
    }

    @GetMapping ("/patient/count")
    public JsonResponse getPatientCount(HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getParameter("user_id"));
        int count = patientService.selectCountByUserId(userId);
        return JsonResponse.success(count);
    }

    @GetMapping("/patient/getDefault")
    @OperateLog("获取默认就诊人")
    public JsonResponse getDefaultPatient(Integer user_id) {
        PatientBean patientBean = patientService.selectDefaultPatient(user_id);
        return JsonResponse.success(patientBean);
    }

}